package mongoc

import (
	"context"
	"log"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var mongodb *mongo.Client

func initMongo() {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	c, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://gin_mongo:27017"))
	if err != nil {
		log.Println("mongo连错误", err)
	}
	mongodb = c
}
func MongoClient() *mongo.Client {
	if mongodb == nil {
		initMongo()
	}
	return mongodb
}
